﻿using MengXunAppTool.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using 软件发布平台.ViewModel;

namespace 软件发布平台.View
{
    /// <summary>
    /// SourceTreeView.xaml 的交互逻辑
    /// </summary>
    public partial class SourceTreeView : UserControl
    {
        ReleaseView releaseView = new ReleaseView();
        List<CheckBoxTreeViewModel> tree2ViewModels;
        public SourceTreeView(ReleaseView release)
        {
            InitializeComponent();
            releaseView = release;
            this.DataContext = release.viewModel;
            Tree2.Items.Clear();
        }

        private void Tree1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                //判断是否选中节点
                TreeViewItem target = releaseView.GetNearestContainer(e.OriginalSource as UIElement);
                if (target != null)
                {
                    releaseView.SelectTreeViewModel = target.Header as CheckBoxTreeViewModel;
                    //实例化菜单
                    System.Windows.Controls.ContextMenu cm = this.FindResource("ContextMenu1") as System.Windows.Controls.ContextMenu;
                    //目标
                    cm.PlacementTarget = sender as TreeViewItem;
                    //位置
                    cm.Placement = PlacementMode.MousePoint;
                    //显示菜单
                    cm.IsOpen = true;
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString() );
            }
        }
        CheckBoxTreeViewModel LastSelectModel;
        private void Tree2_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                TreeViewItem target = releaseView.GetNearestContainer(e.OriginalSource as UIElement);
                if (target != null)
                {
                    target.IsSelected = true;
                    CheckBoxTreeViewModel SelectModel = target.Header as CheckBoxTreeViewModel;
                    if (SelectModel != null)
                    {
                        if (SelectModel.Path.Substring(SelectModel.Path.Length - 4).Equals(".exe"))
                        {
                            if (LastSelectModel != null)
                            {
                                LastSelectModel.ForeColor = new SolidColorBrush(Color.FromRgb(0, 0, 0));
                            }
                            SelectModel.ForeColor = new SolidColorBrush(Color.FromRgb(50, 200, 50));
                            releaseView.exePath = SelectModel.Path;
                            LastSelectModel = SelectModel;
                        }
                    }
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString() );
            }
        }

        private void Tree2_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                //判断是否选中节点
                TreeViewItem target = releaseView.GetNearestContainer(e.OriginalSource as UIElement);
                if (target != null)
                {
                    releaseView.SelectTreeViewModel = target.Header as CheckBoxTreeViewModel;
                    //实例化菜单
                    System.Windows.Controls.ContextMenu cm = this.FindResource("ContextMenu2") as System.Windows.Controls.ContextMenu;
                    //目标
                    cm.PlacementTarget = sender as TreeViewItem;
                    //位置
                    cm.Placement = PlacementMode.MousePoint;
                    //显示菜单
                    cm.IsOpen = true;
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString() );
            }
        }

        private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
        {
            try
            {
                //判断是否选中节点
                TreeViewItem target = releaseView.GetNearestContainer(e.OriginalSource as UIElement);
                if (target != null)
                {
                    CheckBoxTreeViewModel CheckModel = target.Header as CheckBoxTreeViewModel;
                    if (!releaseView.configFiles.Contains(CheckModel.Path))
                    {
                        releaseView.configFiles.Add(CheckModel.Path);
                    }
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString() );
            }
        }

        private void CheckBox_Unchecked_1(object sender, RoutedEventArgs e)
        {
            try
            {
                //判断是否选中节点
                TreeViewItem target = releaseView.GetNearestContainer(e.OriginalSource as UIElement);
                if (target != null)
                {
                    CheckBoxTreeViewModel CheckModel = target.Header as CheckBoxTreeViewModel;
                    if (releaseView.configFiles.Contains(CheckModel.Path))
                    {
                        releaseView.configFiles.Remove(CheckModel.Path);
                    }
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString() );
            }
        }

        private void TextBlockSelect_MouseLeftButtonUp(object sender, RoutedEventArgs e)
        {
            try
            {
                if (releaseView.SelectTreeViewModel != null)
                {
                    tree2ViewModels = new List<CheckBoxTreeViewModel>();
                    releaseView.exeBasePath = releaseView.SelectTreeViewModel.Path;
                    releaseView.viewModel.InitTree(releaseView.exeBasePath, releaseView.exeFiles, tree2ViewModels, releaseView.removeExeFiles, releaseView.configFiles, releaseView.exePath);
                    Tree2.ItemsSource = tree2ViewModels;
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString() );
            }
        }

        private void TextBlockRemove_MouseLeftButtonUp(object sender, RoutedEventArgs e)
        {
            try
            {
                if (releaseView.SelectTreeViewModel != null)
                {
                    releaseView.removeFiles.Add(releaseView.SelectTreeViewModel.Path);
                    releaseView.viewModel.Items.Clear();
                    releaseView.files.Clear();
                    List<CheckBoxTreeViewModel> treeViewModels = new List<CheckBoxTreeViewModel>();
                    releaseView.viewModel.InitTree(releaseView.basePath, releaseView.files, treeViewModels, releaseView.removeFiles);
                    releaseView.viewModel.Items = treeViewModels;
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString() );
            }
        }

        private void TextBlockRemove_MouseLeftButtonUp2(object sender, RoutedEventArgs e)
        {
            try
            {
                if (releaseView.SelectTreeViewModel != null)
                {
                    releaseView.removeExeFiles.Add(releaseView.SelectTreeViewModel.Path);
                    releaseView.exeFiles.Clear();
                    tree2ViewModels = new List<CheckBoxTreeViewModel>();
                    releaseView.viewModel.InitTree(releaseView.exeBasePath, releaseView.exeFiles, tree2ViewModels, releaseView.removeExeFiles,releaseView.configFiles,releaseView.exePath);
                    Tree2.ItemsSource = tree2ViewModels;
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString() );
            }
        }

    }
}
